/**
 * @Project : ProgrammingExercises
 * @Package : Leetcode
 * @File : Q0234.java
 * @Author : WangRuoyu
 * @Date 2022/12/26 19:11
 */

package leetcode;

import util.LinkList;
import util.ListNode;

public class Q0234 {
    public boolean isPalindrome(ListNode head) {
        if ((head == null) || (head.next == null)) {
            return true;
        }

        ListNode p = null;
        ListNode s = head;

        while (s != null) {
            ListNode k = new ListNode(s.val);
            k.next = p;
            p = k;
            s = s.next;
        }

        s = head;
        while (s != null) {
            if (s.val != p.val) {
                return false;
            }
            s = s.next;
            p = p.next;
        }
        return true;
    }

    public static void main(String[] args) {
        Q0234 sl = new Q0234();
        ListNode head = LinkList.createList(new int[]{1, 2, 2, 1});
        System.out.println(sl.isPalindrome(head));
    }
}
